package etc

import (
	"ArticleServer/api/internal/config"
	"context"
	clientv3 "go.etcd.io/etcd/client/v3"
	"log"
	"time"
)

func DiscoverEtcdService(etcd *config.Etcd) {
	// 建立连接
	etcdCli, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{"localhost:2379"}, //我这里是单节点，并非集群哦
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		log.Fatal(err)
	}
	// 运行完之后结束etcd服务
	defer etcdCli.Close()

	// 对应注册的服务名
	key := "/services/" + etcd.Name

	// 得到响应
	resp, err := etcdCli.Get(context.Background(), key)

	// 把内容给配置
	etcd.Addr = string(resp.Kvs[0].Value)
}
